﻿using System.Collections.Generic;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCS.UI
{
    public partial class ZMCS60 : EFViewCmes
    {
        private EFTreeItem item;//记录选中的树节点

        public ZMCS60()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ZMCS60_Loaded);
        }

        private void ZMCS60_Loaded(object sender, RoutedEventArgs e)
        {
            this.projectModelTree.Refresh();
        }

        /// <summary>
        /// 选择树节点，右侧页面刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void projectModelTree_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            item = this.projectModelTree.SelectedItems[0] as EFTreeItem;
            string fdKeyId = item.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;//主键
            string fdParentName = item.ItemObject.GetType().GetProperty("display").GetValue(item.ItemObject, null) as string;//名称
            string isLeaf = (item.IsLeaf == true) ? "1" : "0";//是否叶子节点
            string level = item.ItemObject.GetType().GetProperty("level").GetValue(item.ItemObject, null) as string;//树的级别
            string nodeTypes = item.ItemObject.GetType().GetProperty("nodeTypes").GetValue(item.ItemObject, null) as string;//节点类型

            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("fdParentId", fdKeyId);
            args.Add("fdParentName", fdParentName);
            args.Add("isLeaf", isLeaf);
            args.Add("level", level);
            args.Add("nodeTypes", nodeTypes);
            
            //工程为叶子节点
            if (isLeaf == "1")
            {
                switch (nodeTypes)
                {
                    case "1":
                        //节点为工程设计，下级为工作面
                        this.container.LoadView("ZMCS62", args);
                        break;
                    case "2":
                        //节点为工程设计，下级为巷道
                        this.container.LoadView("ZMCS63", args);
                        break;
                    case "3":
                        //节点为工程设计，下级为其他工程设计
                        this.container.LoadView("ZMCS64", args);
                        break;
                    default:
                        break;
                }
            }
            else
            {
                //节点为矿井，下级为工程设计
                this.container.LoadView("ZMCS61", args);
            }
        }

        /// <summary>
        /// 重新基类的发布数据方法
        /// 刷新树节点
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(Platform.Core.EIInfo data)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string _value = "";
            if (item != null)
            {
                if (!item.IsLeaf)
                {
                    _value = item.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;
                    args.Add("node", _value);
                    _value = (item.IsLeaf == true) ? "1" : "0";//是否叶子节点
                    args.Add("isLeaf", _value);
                    _value = item.ItemObject.GetType().GetProperty("level").GetValue(item.ItemObject, null) as string;//树的级别
                    args.Add("level", _value);
                    _value = item.ItemObject.GetType().GetProperty("nodeTypes").GetValue(item.ItemObject, null) as string;//节点类型
                    args.Add("nodeTypes", _value);
                    BaseTools.RefreshTree("ZMCS60", "query", args, OnDoWorkCompleted);
                }
            }
        }

        /// <summary>
        /// OnEIDataPublished回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.projectModelTree.EIDataContext = new Platform.Core.UI.EIDataContext();
            this.projectModelTree.EIDataContext.Update(e.EIInfo);
            item.IsLoadOnDemandEnabled = true;
            item.Items.Clear();
            item.CollapseItem();
            item.ExpandItem();
        }

        /// <summary>
        /// 第一次进入页面时选中树的根节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void projectModelTree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            if (treeItem.Level == 0 && treeItem.Index==0)
            {
                this.projectModelTree.SelectedItem = treeItem;//选中
                treeItem.ExpandItem();//展开
            }
        }
    }
}
